function person(){
    this.point=new Point();
    this.face=0;
    this.frame=0;
}

function Info() {
    this.health=10;
    this.power=50;
    this.diedTimes=0;
    this.kill=0;
}

function draw(){
    if(this.imgCode==1){
        return;
    }else if(this.imgCode==0){
        this.frame=0;
        this.face=0;
    }
    var img=personImgs[this.imgCode];
    cxt.drawImage(img.img,this.frame*img.width,this.face*img.height,img.width,img.height,this.point.x-img.width/2,this.point.y-img.height/2-30,img.width,img.height);
}

function drawBang(i){
    if(typeof this.frame == "undefined")this.frame=0;
    if(typeof this.face == "undefined")this.face=0;
    cxt.drawImage(bangImg.img,this.frame*bangImg.width,this.face*bangImg.height,bangImg.width,bangImg.height,this.point.x-bangImg.width/2,this.point.y-bangImg.height/2,bangImg.width,bangImg.height);
    this.frame++;
    if(this.frame==bangImg.x){
        this.frame=0;
        this.face++;
        if(this.face==bangImg.y)bangs.splice(i,1);
    }
}

function control(){
    var speed=1;
    var deltaSpeed=speed*Math.sin(Math.PI/4);
    if (speedUp&&keyDown){
        speed*=3;
        deltaSpeed*=3;
        this.imgCode=MyImgCode+1;
    }else{
        this.imgCode=MyImgCode;
    }
    if(keyDown){
        loseTime+=deltaTime;
        var ot=1000/60;
        while(loseTime>=ot){
            if(once==Myimg.step){
                once=0;
                this.frame++;
                if(this.frame>=Myimg.x)this.frame=0;
            }else {
                once++;
            }
            loseTime-=ot;
            var tempPoint=new Point();
            tempPoint.x=this.point.x;
            tempPoint.y=this.point.y;
            switch (this.face){
                case 0:
                    this.point.y+=speed;
                    break;
                case 1:
                    this.point.x-=speed;
                    break;
                case 2:
                    this.point.x+=speed;
                    break;
                case 3:
                    this.point.y-=speed;
                    break;
                case 4:
                    this.point.x-=deltaSpeed;
                    this.point.y+=deltaSpeed;
                    break;
                case 5:
                    this.point.x+=deltaSpeed;
                    this.point.y+=deltaSpeed;
                    break;
                case 6:
                    this.point.x-=deltaSpeed;
                    this.point.y-=deltaSpeed;
                    break;
                case 7:
                    this.point.x+=deltaSpeed;
                    this.point.y-=deltaSpeed;
                    break;
            }
            if(isInWall(this.point)){
                this.point=tempPoint;
            }

        }
    }else {
        this.frame=0;
    }
}

function isInWall(p) {
        if(p.x<20||p.x>980||p.y<40||p.y>960){
        return true;
    }
}


function drawMySign() {
    cxt.beginPath();
    cxt.fillStyle="#e11";
    cxt.moveTo(I.point.x,I.point.y+45);
    cxt.lineTo(I.point.x+10,I.point.y+55);
    cxt.lineTo(I.point.x-10,I.point.y+55);
    cxt.closePath();
    cxt.fill();
}

function drawName() {
    cxt.beginPath();
    cxt.fillStyle="#222";
    cxt.textAlign="center";
    cxt.textBaseline="middle";
    cxt.fillText(this.id,this.point.x,this.point.y-52);
    drawPlayersHealthAndPowerBackGround.call(this);
    drawPlayersHealthAndPower.call(this);
}

function drawInfo() {
    healthNode.style.width=Math.floor(info.health/10*100)+"%";
    powerNode.style.width=Math.floor(info.power/100*100)+"%";
    killNode.innerText="击杀："+info.kill;
    diedNode.innerText="死亡："+info.diedTimes;
    if(info.health<=0&&resetTime<=0&&MyImgCode!=0){
        MyImgCode=0;
        Myimg=personImgs[MyImgCode];
        if(resetTime<=0){
            resetTime=2900;
            reset();
        }
    }
}

function reset(){
    setTimeout(function(){
        revivalAble=true;
    },3000);
}

function revival() {
    if(!revivalAble)return;
    MyImgCode= I.imgCode=MyCode;
    Myimg=personImgs[MyImgCode];
    I.point=new Point();
    revivalAble=false;
}

function drawPlace(r,g,b,a){
    for(var i = 0 ; i < this.length ; ++i ){
        if(this[i].health<=0)continue;
        cxt.save();
        cxt.beginPath();
        cxt.translate(this[i].point.x,this[i].point.y);
        cxt.scale(1,.5);
        var crg=cxt.createRadialGradient(0,0,5,0,0,this[i].health+40);
        crg.addColorStop(0,"rgba("+r+","+g+","+b+","+a+")");
        crg.addColorStop(.3,"rgba("+Math.floor(r/2)+","+Math.floor(g/2)+","+Math.floor(b/2)+","+Math.floor(a/2)+")");
        crg.addColorStop(.7,"rgba("+Math.floor(r *.7)+","+ Math.floor(g*.7)+","+Math.floor(b *.7)+","+Math.floor(a *.7)+")");
        crg.addColorStop(1,"rgba(0,0,0,0)");
        cxt.fillStyle=crg;
        cxt.arc(0,0,this[i].health+40,0,2*Math.PI);
        cxt.fill();
        cxt.restore()
    }
}

function drawPlayersHealthAndPowerBackGround(){
    cxt.beginPath();
    cxt.fillStyle="rgba(0,0,0,.3)";
    cxt.fillRect(this.point.x-26,this.point.y-45,52,7);
}

function drawPlayersHealthAndPower(){
    cxt.beginPath();
    cxt.fillStyle="rgba(0,255,0,.7)";
    cxt.fillRect(this.point.x-25,this.point.y-44,this.info.health*5,2);
    cxt.beginPath();
    cxt.fillStyle="rgba(0,0,255,.7)";
    cxt.fillRect(this.point.x-25,this.point.y-41,this.info.power/2,2);
}
